package com.lanjiu.lib.business.task;

import com.lanjiu.pro.file.OriginalPictureVideoFriendsTransmission;
import com.lanjiu.pro.file.OriginalPictureVideoGroupTransmission;
import com.lanjiu.lib.business.FileTransmissionCache;
import com.lanjiu.lib.business.TransmissionConstant;
import com.lanjiu.lib.business.handler.FileSenderHandler;
import com.lanjiu.lib.business.listener.FileTransmissionListener;
import com.lanjiu.lib.business.listener.OnTransmissionFinishListener;
import com.lanjiu.lib.business.option.FileTransmissionSender;
import com.lanjiu.lib.utils.sp.SharedPreferenceHelper;

import java.io.File;

/**
 * @description: 上传任务
 * @author: Panwei
 * @date: 2019/11/4 14:03
 */
public class UploadTask extends TransmissionTask implements Runnable {

    private String mTargetPath;
    private FileSenderHandler mFileSenderHandler;
    private SharedPreferenceHelper mSPHelper;

    private OriginalPictureVideoFriendsTransmission mFriend;
    private OriginalPictureVideoGroupTransmission mGroup;
    private boolean isGroup;

    public static UploadTask newTask(String targetPath, FileTransmissionListener.OnFileSenderListener listener, OriginalPictureVideoFriendsTransmission friend) {
        if (contains(targetPath)) {
            return null;
        } else {
            add(targetPath);
            return new UploadTask(targetPath, listener, friend);
        }
    }

    public static UploadTask newTask(String targetPath, FileTransmissionListener.OnFileSenderListener listener, OriginalPictureVideoGroupTransmission group) {
        if (contains(targetPath)) {
            return null;
        } else {
            add(targetPath);
            return new UploadTask(targetPath, listener, group);
        }
    }

    private UploadTask(String targetPath, FileTransmissionListener.OnFileSenderListener listener) {
        this.mTargetPath = targetPath;
        this.mFileSenderHandler = new FileSenderHandler(listener);
        this.mSPHelper = SharedPreferenceHelper.create(TransmissionConstant.FILE_TRANSMISSION_SP_NAME);
    }

    private UploadTask(String targetPath, FileTransmissionListener.OnFileSenderListener listener, OriginalPictureVideoFriendsTransmission friend) {
        this(targetPath, listener);
        this.mFriend = friend;
        this.isGroup = false;
    }

    private UploadTask(String targetPath, FileTransmissionListener.OnFileSenderListener listener, OriginalPictureVideoGroupTransmission group) {
        this(targetPath, listener);
        this.mGroup = group;
        this.isGroup = true;
    }

    @Override
    public void run() {
        File targetFile = new File(mTargetPath);
        if (!targetFile.exists()) {
            if (mFileSenderHandler != null)
                mFileSenderHandler.sendErrorMessage(TransmissionConstant.TRANSMISSION_FILE_NOT_EXISTS, "文件不存在");
            remove(mTargetPath);
            return;
        }

        FileTransmissionSender sender = new FileTransmissionSender(mSPHelper, mFileSenderHandler);
        sender.setOnTransmissionFinishListener(getTransmissionFinishListener());

        long length = targetFile.length();
        FileTransmissionCache mFTCache;
        if (mSPHelper.contains(mTargetPath)) {
            // 断点上传
            mFTCache = mSPHelper.getObject(mTargetPath, FileTransmissionCache.class);
            if (mFTCache == null || mFTCache.getTotalLength() != length) {
                int count = (int) (length % TransmissionConstant.FRAGMENT_LENGTH == 0 ? length / TransmissionConstant.FRAGMENT_LENGTH : length / TransmissionConstant.FRAGMENT_LENGTH + 1);
                mFTCache = new FileTransmissionCache(count, length, mTargetPath);
            }
        } else {
            // 新的上传
            int count = (int) (length % TransmissionConstant.FRAGMENT_LENGTH == 0 ? length / TransmissionConstant.FRAGMENT_LENGTH : length / TransmissionConstant.FRAGMENT_LENGTH + 1);
            mFTCache = new FileTransmissionCache(count, length, mTargetPath);
        }

        if (isGroup)
            sender.transmissionFileGroupSend(mFTCache, mGroup);
        else
            sender.transmissionFilePersonSend(mFTCache, mFriend);
    }

    @Override
    protected OnTransmissionFinishListener getTransmissionFinishListener() {
        return TransmissionTask::remove;
    }
}